package com.leyou.item.web;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryService;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     *
     * 根据分类id，查询商品分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") Long id){
        Category category = categoryService.getById(id);
        CategoryDTO categoryDTO=new CategoryDTO(category);
        return ResponseEntity.status(HttpStatus.OK).body(categoryDTO);

        //return ResponseEntity.ok(new CategoryDTO(categoryService.getById(id)));
    }

    /**
     * 根据分类id的集合，查询商品分类的集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids") List<Long> ids){
        //List<Category> categoryList = categoryService.listByIds(ids);
        //List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(categoryList);

        return ResponseEntity.ok(CategoryDTO.convertEntityList(categoryService.listByIds(ids)));

    }

    /**
     *
     根据父类目id，查询子类目的集合
     * @param pid
     * @return
     */
    @GetMapping("/of/parent")
        public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam("pid") Long pid){
            List<Category> categories = categoryService.query().eq("parent_id", pid).list(); //条件查询
            List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(categories);
            return ResponseEntity.status(HttpStatus.OK).body(categoryDTOS);
    }

    /**
     * 根据品牌id，查询商品分类的集合
     * @param brandId
     * @return
     */
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByBrandId(@RequestParam("id") Long brandId){
        List<CategoryDTO> dtoList = categoryService.queryCategoryByBrandId(brandId);
        return ResponseEntity.ok(dtoList);
    }

}
